var baseURL = "http://beta.ffxivspecs.com";

$(window).load(function() {
	// Whe coming from a new browser window

	var url = window.location.hash.substring(2);
	var params = "";
	var show = "";
	var parameterStart = url.indexOf("layout");
	if(parameterStart != -1){
		// calendar append parameters if any
		params = {layout: url.substring(parameterStart+7)};
		show = "/layout/"+params.layout;
		url = "calculator";
		
	}
	var parameterStart = url.indexOf("user");
	if(parameterStart != -1){
		// calendar append parameters if any
		params = {userName: url.substring(parameterStart+5)};
		show = "/user/"+params.userName;
		url = "user";
	}
	var parameterStart = url.indexOf("spec");	
	if(parameterStart != -1){
		// calendar append parameters if any
		params = {spec: url.substring(parameterStart+5)};
		show = "/spec/"+params.spec;
		url = "calculator";
	}

	if (url == "" || url == "login" ){
		var urlVars = getUrlVars();
		if(urlVars['layout'] != undefined || urlVars['spec'] != undefined){
			url = "calculator";
			show = "";
			params = {layout: urlVars['layout'], spec: urlVars['spec']}
		}else{
			url = "main";
			show = "/"+url;
		}
		
		
	}
	if(show=="" && url != "calculator"){
		show = "#/"+url;
	}
	loadURL(url, params, show);
});

$(document).ready(function(){
	$('#menu-item-list > li').click(function(e) {
		var clicked = jQuery(e.target);
		var url = clicked.text().toLowerCase();
		if(url == "me"){
			loadURL("user", {userName: "me"}, "#/user/me");
		}else{
			loadURL(url, null, "#/"+url);
		}
		
	});
	$('#bug-icon').click(function(e) {
		loadURL("report", null, "#/report");
	
	});
	
});

function getUrlVars(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function loadURL(url, params, show){
	
	if(url == "login"){
		$('#login-container').addClass('blue-border');
	}else if(url == "logout"){
		logoutUser();
	}else{
		$("#left-content-container").html("<div class=\"center\"><img class=\"main-loader-image\" src=\"media/loader.gif\"></img></div>")
		$('#login-container').removeClass('blue-border');
		$("#left-content-container").load("./"+url+".php", params, function(response, status, xhr) {
			$("#left-content-container").hide();
			$("#left-content-container").fadeIn('fast');
			if (status == "error") {
				var msg = "Sorry but there was an error: ";
				$("#left-content-container").html(msg + xhr.status + " " + xhr.statusText);
			}
		});
	}
	if(url != "logout"){
		colorMenu(url);
	}
	
	window.location.hash =  show;
	
};

function colorMenu(url){

	$('li').each(function(index) {
		if($(this).text().toLowerCase() == url){
			$(this).addClass("menu-item-selected");
		}else{
			$(this).removeClass("menu-item-selected");
		}
	});
	// Special handle me cause it is not loaded yet
	if(url == "user" || url == "user_profile"){
		$('#login-menu-item').addClass("menu-item-selected");
	}
}

function getHashTag(){
	return window.location.hash;
}

function loadHotSpecs(){
	$('#hot-list').html('<div class=\"center\"><img class=\"hottest-specs-loader-image\" src=\"media/loader.gif\"></img></div>');
	$('#hot-list').load("hot_specs.php" ,{} , function(response, status, xhr) {
	  if (status == "error") {
	    var msg = "Sorry but there was an error: ";
	    $('#hot-list').html(msg + xhr.status + " " + xhr.statusText);
	  }else{
		$("#hot-list").hide();
		$("#hot-list").fadeIn('fast');
	  }
	});
}
function loadTwitterFeed(){
	$('#twitter-detail-content').html('<div class=\"center\"><img class=\"twitter-feed-loader-image\" src=\"media/loader.gif\"></img></div>');
	$('#twitter-detail-content').load("php/twitter_feed.php" ,{} , function(response, status, xhr) {
	  if (status == "error") {
	    var msg = "Sorry but there was an error: ";
	    $('#twitter-detail-content').html(msg + xhr.status + " " + xhr.statusText);
	  }else{
		$("#twitter-detail-content").hide();
		$("#twitter-detail-content").fadeIn('fast');
	  }
	});
}

